// // pages/serve/chapter/chapter.js setTime
var mistakesArr = []//错题
var correct = 0
//触屏开始点坐标
var startDot = {
  X: 0,
  Y: 0
};
//触屏到点坐标
var touchDot = {
  X: 0,
  Y: 0
};
var time2 = 0;  //触屏时间
var number;     //定时器ID
var newIndex = 0//设置活动菜单的index
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  
  data: {
    bar_Height: wx.getSystemInfoSync().statusBarHeight + 45,
    chapterName:"章节练习",
    title: "1",
    chapterClickTops:true,
    topicArr:[],
    topicArrs:[],
    gif: "true",
    topicObj: {},
    topicArrInd:0,//正在做的题的数组下标
    topicYes:0,//正确
    topicNo:0,//错误
    view1: 'selection1',
    view2: 'selection1',
    view3: 'selection1',
    view4: 'selection1',
    falseOption: ['', '', '', ''],
    tureOption: ['', '', '', ''],
    options: ["A", "B", "C", "D"],
    videoNum:false,
    types:1,
    // chapter:false

  },
  //模式选择
  chapterTitle(e) {
    var that = this
    // console.log(e.currentTarget.dataset.index)
    if (e.currentTarget.dataset.index == 1){

    }
    that.setData({
      title: e.currentTarget.dataset.index
    })
  },
  //关闭视频
  videoClose(e){
    this.setData({
      videoNum: false,
    })
  },
  //打开视频
  videoOpen(e) {
    this.setData({
      videoNum: true,
    })
  },
  /*选择(单选) */
  viewClick: function (e) {
    var that = this
    // console.log(e)
    var select = e.currentTarget.dataset.id
    var indexs = e.currentTarget.dataset.index
    var c_id = e.currentTarget.dataset.cid
   

    // console.log(c_id, that.data.topicArr[indexs])
    // console.log(c_id, indexs, select)

    // 选项没被选择时将执行 
    if (that.data.topicArr[indexs].my_yes == '' || !that.data.topicArr[indexs].my_yes) {
      // 将选项设置为“已被选择” 
      var arr = that.data.topicArr
      let newObj = arr[indexs]
      // let mytopics = JSON.stringify(topicObj.topics)
      // topicObj.my_yes = topicObj.topics
      newObj.result = select
      newObj.my_yes = select
      arr.splice(indexs, 1, newObj)

      let obj = that.data.topicObj
      obj.my_yes = select
      let topicYes = that.data.topicYes
      let topicNo = that.data.topicNo
      if (select == newObj.item_true) {
        topicYes++
        answer(c_id, 1, select, indexs)
      } else {
        topicNo++
        answer(c_id, 2, select, indexs)
        errors(c_id)
        // mistakesArr.push(c_id)
      }
      that.setData({
        topicObj: obj,
        topicArr: arr,
        topicYes: topicYes,
        topicNo: topicNo,
      })
    }
    // console.log(that.data.arrTitle)
  },
  /*选择(多选) */
  viewClick2(e) {
    var that = this
    let topicObj = that.data.topicObj
    // console.log(e.currentTarget.dataset.topic, e.currentTarget.dataset.id)
    if (e.currentTarget.dataset.topic == 'A') {
      if (topicObj.topics[0] == "") {
        topicObj.topics[0] = {
          name: e.currentTarget.dataset.topic,
          content: e.currentTarget.dataset.id
        }
      } else {
        topicObj.topics[0] = ''
      }
    }
    if (e.currentTarget.dataset.topic == 'B') {
      if (topicObj.topics[1] == "") {
        topicObj.topics[1] = {
          name: e.currentTarget.dataset.topic,
          content: e.currentTarget.dataset.id
        }
      } else {
        topicObj.topics[1] = ''
      }
    }
    if (e.currentTarget.dataset.topic == 'C') {
      if (topicObj.topics[2] == "") {
        topicObj.topics[2] = {
          name: e.currentTarget.dataset.topic,
          content: e.currentTarget.dataset.id
        }
      } else {
        topicObj.topics[2] = ''
      }
    }
    if (e.currentTarget.dataset.topic == 'D') {
      if (topicObj.topics[0] == "") {
        topicObj.topics[3] = {
          name: e.currentTarget.dataset.topic,
          content: e.currentTarget.dataset.id
        }
      } else {
        topicObj.topics[3] = ''
      }
    }
    that.setData({
      topicObj: topicObj
    })

  },

  // my_yes_ture =>result_ture
  /**多选确定 */
  buttonYes(e) {
    var that = this
    let topicObj = that.data.topicObj
    let words = topicObj.item_true
    let newKeyArr = []
    let result_ture = []
    let c_id = e.currentTarget.dataset.cid
    // console.log(topicObj.topics)
    let nua = 0
    for (let p = 0; p < topicObj.topics.length; p++) {
      if (topicObj.topics[p] != '') {
        nua++
      }
    }
    if (nua < 2) {
      wx.showToast({
        title: "至少选择两项",
        icon: 'none',
        duration: 1000
      })
    } else {
      

      let mytopics = JSON.stringify(topicObj.topics)
      topicObj.my_yes = topicObj.topics
      topicObj.result = mytopics

      // console.log(topicObj)
      var indexs = e.currentTarget.dataset.index
      for (let j = 0; words.indexOf("|") + 1 > 0; j++) {
        let newKey = words.slice(0, words.indexOf("|"))
        words = words.slice(words.indexOf("|") + 1)
        newKeyArr.push(newKey)
        if (words.indexOf("|") < 0) {
          newKeyArr.push(words)
        }
      }
      // 
      for (let g = 0; g < topicObj.option.length; g++) {
        let optionTrue = false
        let optionContent = ''
        for (let f = 0; f < newKeyArr.length; f++) {
          if (topicObj.option[g] == newKeyArr[f]) {
            optionTrue = true
            optionContent = newKeyArr[f]
          }
        }
        if (optionTrue) {
          result_ture.push(optionContent)
        } else {
          result_ture.push('')
        }
      }
      // console.log(result_ture, "result_ture")
      topicObj.result_ture = result_ture
      let num = 0
      // console.log(newKeyArr, topicObj.topics)
      for (let x = 0; x < newKeyArr.length; x++) {
        for (let y = 0; y < topicObj.topics.length; y++) {
          if (newKeyArr[x] == topicObj.topics[y].content) {
            num++
          }
        }
      }
      let topicYes = that.data.topicYes
      let topicNo = that.data.topicNo
      // console.log(c_id, 1, topicObj.topics, indexs)
      // let mytopics = JSON.stringify(topicObj.topics)
      // console.log(mytopics)
      // let arrs1 = that.data.arrs1
      // let arrs2 = that.data.arrs2
      // let arrs3 = that.data.arrs3
      // let arrs4 = that.data.arrs4
      // let arrs5 = that.data.arrs5
      // if (indexs < 300) {
      //   if (num == newKeyArr.length) {
      //     arrs1[indexs].status = 1
      //   } else {
      //     arrs1[indexs].status = 2
      //   }
      //   that.setData({
      //     arrs1: arrs1
      //   })
      // }
      // if (indexs < 600 && indexs > 299) {
      //   if (num == newKeyArr.length) {
      //     arrs2[indexs - 300].status = 1
      //   } else {
      //     arrs2[indexs - 300].status = 2
      //   }
      //   that.setData({
      //     arrs2: arrs2
      //   })
      // }
      // if (indexs < 900 && indexs > 599) {
      //   if (num == newKeyArr.length) {
      //     arrs3[indexs - 600].status = 1
      //   } else {
      //     arrs3[indexs - 600].status = 2
      //   }
      //   that.setData({
      //     arrs3: arrs3
      //   })
      // }
      // if (indexs < 1200 && indexs > 899) {
      //   if (num == newKeyArr.length) {
      //     arrs4[indexs - 900].status = 1
      //   } else {
      //     arrs4[indexs - 900].status = 2
      //   }
      //   that.setData({
      //     arrs4: arrs4
      //   })
      // }
      // if (indexs > 1200) {
      //   if (num == newKeyArr.length) {
      //     arrs5[indexs - 1200].status = 1
      //   } else {
      //     arrs5[indexs - 1200].status = 2
      //   }
      //   that.setData({
      //     arrs5: arrs5
      //   })
      // }
      if (num == newKeyArr.length) {
        topicObj.yesORno = 1
        topicYes++
        answer(c_id, 1, mytopics, indexs)

      } else {
        topicObj.yesORno = 2
        answer(c_id, 2, mytopics, indexs)
        errors(c_id)

        topicNo++
        mistakesArr.push(c_id)
      }

      var arr = that.data.topicArr
      arr.splice(indexs, 1, topicObj)
      // console.log(topicObj)
      that.setData({
        topicObj: topicObj,
        topicYes: topicYes,
        topicNo: topicNo,
        topicArr: arr,

      })
    }

  },


  //上一页
  previous(e){
    var that = this
    // if (that.data.types == 2) {
    //   wx.showToast({
    //     title: "正在加载",
    //     icon: "loading",
    //     mask: "ture",
    //     duration: 1200
    //   })
    // }

    let arrs = that.data.topicArr
    if (newIndex > 0) {
      // console.log("上一页")
      newIndex = newIndex - 1
      time2 = 1;

      let topicObj = arrs[newIndex]
      if (!topicObj.my_yes) {
        topicObj.my_yes = ''
      }
      let optionArr = []
      if (topicObj.item1 != '') {
        optionArr.push(topicObj.item1)
      }
      if (topicObj.item2 != '') {
        optionArr.push(topicObj.item2)
      }
      if (topicObj.item3 != '') {
        optionArr.push(topicObj.item3)
      }
      if (topicObj.item4 != '') {
        optionArr.push(topicObj.item4)
      }
      topicObj.option = optionArr
      let topicObjContent = topicObj.content//获取题目
      let topicObjKey = topicObj.key_words//获取关键字
      let falseKey = topicObj.key_false//获取关键字
      let tureKey = topicObj.key_true//获取关键字
      if (topicObjKey) {
        keyWords(that, topicObjContent, topicObjKey)
      } else {
        that.setData({
          arrTitle: [],
        })
      }

      if (falseKey != '') {
        falseKeys(that, falseKey, topicObj.option)
      } else {
        that.setData({
          falseOption: ['', '', '', '']
        })
      }
      if (tureKey != '') {
        tureKeys(that, tureKey, topicObj.option)
      } else {
        that.setData({
          tureOption: ['', '', '', '']

        })
      }
      if (arrs[newIndex].culture == 1) {
        arrs[newIndex].topics = ['', '', '', '']
      }

      let words = topicObj.item_true
      let newKeyArr = []
      let result_ture = []
      for (let j = 0; words.indexOf("|") + 1 > 0; j++) {
        let newKey = words.slice(0, words.indexOf("|"))
        words = words.slice(words.indexOf("|") + 1)
        newKeyArr.push(newKey)
        if (words.indexOf("|") < 0) {
          newKeyArr.push(words)
        }
      }
      // 
      for (let g = 0; g < topicObj.option.length; g++) {
        let optionTrue = false
        let optionContent = ''
        for (let f = 0; f < newKeyArr.length; f++) {
          if (topicObj.option[g] == newKeyArr[f]) {
            optionTrue = true
            optionContent = newKeyArr[f]
          }
        }
        if (optionTrue) {
          result_ture.push(optionContent)
        } else {
          result_ture.push('')
        }
      }
      if (app.chapterStudy == 1){
        if (topicObj.result || topicObj.result != '') {
          topicObj.my_yes = topicObj.result
        }
      }
      if (topicObj.culture == 1 && (topicObj.result || topicObj.result != '')) {
        topicObj.my_yes = JSON.parse(topicObj.result)
      } else {
        topicObj.my_yes = topicObj.result
      }
      
      topicObj.result_ture = result_ture
      // console.log(arrs[newIndex])
      // console.log(topicObj)

      that.setData({
        topicArr: arrs,
        // topicObj: arrs[newIndex],
        topicObj: topicObj,
        view1: 'selection1',
        view2: 'selection1',
        view3: 'selection1',
        view4: 'selection1',
        topicArrInd: newIndex,
      })
    } else {
      wx.showToast({
        title: "这已经是第一题了",
        icon: 'none',
        duration: 1000
      })
    }
  },
  //下一页
  next(e){
    var that = this
    let arrs = that.data.topicArr
    // console.log(that.data.topicArr)
    // console.log(newIndex)


    if (newIndex < arrs.length - 1) {
      // console.log("下一页")
      // if (that.data.types == 2) {
      //   wx.showToast({
      //     title: "正在加载",
      //     icon: "loading",
      //     mask: "ture",
      //     duration: 1200
      //   })
      // }

      time2 = 2;
      newIndex = newIndex + 1
      let topicObj = arrs[newIndex]
      if (!topicObj.my_yes) {
        topicObj.my_yes = ''
      }
      let optionArr = []
      if (topicObj.item1 != '') {
        optionArr.push(topicObj.item1)
      }
      if (topicObj.item2 != '') {
        optionArr.push(topicObj.item2)
      }
      if (topicObj.item3 != '') {
        optionArr.push(topicObj.item3)
      }
      if (topicObj.item4 != '') {
        optionArr.push(topicObj.item4)
      }
      topicObj.option = optionArr
      let topicObjContent = topicObj.content//获取题目
      let topicObjKey = topicObj.key_words//获取关键字
      let falseKey = topicObj.key_false//获取关键字
      let tureKey = topicObj.key_true//获取关键字
      if (topicObjKey) {
        keyWords(that, topicObjContent, topicObjKey)
      } else {
        that.setData({
          arrTitle: [],
        })
      }
      if (falseKey != '') {
        falseKeys(that, falseKey, topicObj.option)
      } else {
        that.setData({
          falseOption: ['', '', '', '']
        })
      }
      if (tureKey != '') {
        tureKeys(that, tureKey, topicObj.option)
      } else {
        that.setData({
          tureOption: ['', '', '', '']
        })
      }
      if (arrs[newIndex].culture == 1) {
        arrs[newIndex].topics = ['', '', '', '']
      }
      let words = topicObj.item_true
      let newKeyArr = []
      let result_ture = []
      for (let j = 0; words.indexOf("|") + 1 > 0; j++) {
        let newKey = words.slice(0, words.indexOf("|"))
        words = words.slice(words.indexOf("|") + 1)
        newKeyArr.push(newKey)
        if (words.indexOf("|") < 0) {
          newKeyArr.push(words)
        }
      }
      // 
      for (let g = 0; g < topicObj.option.length; g++) {
        let optionTrue = false
        let optionContent = ''
        for (let f = 0; f < newKeyArr.length; f++) {
          if (topicObj.option[g] == newKeyArr[f]) {
            optionTrue = true
            optionContent = newKeyArr[f]
          }
        }
        if (optionTrue) {
          result_ture.push(optionContent)
        } else {
          result_ture.push('')
        }
      }

      topicObj.result_ture = result_ture
      if (app.chapterStudy == 1) {
        if (topicObj.result || topicObj.result != '') {
          topicObj.my_yes = topicObj.result
        }
      }
      if (topicObj.culture == 1 && (topicObj.result || topicObj.result != '')) {
        topicObj.my_yes = JSON.parse(topicObj.result)
      } else {
        topicObj.my_yes = topicObj.result
      }
      // console.log(arrs[newIndex])
      // console.log(topicObj)
      that.setData({
        topicArr: arrs,
        // topicObj: arrs[newIndex],
        topicObj: topicObj,
        view1: 'selection1',
        view2: 'selection1',
        view3: 'selection1',
        view4: 'selection1',
        topicArrInd: newIndex,

      })
    }else{
      wx.showToast({
        title: "这已经是最后一题了",
        icon: 'none',
        duration: 1000
      })
    }

  },


  //题号查看
  chapterClickTop(e) {
    var that = this
    var chapterClickTops = that.data.chapterClickTops
    let widthBL = 750 / wx.getSystemInfoSync().windowWidth
    let heightWin = wx.getSystemInfoSync().windowHeight / 2 - (80 / widthBL)
    var animation2 = wx.createAnimation({});
    if (chapterClickTops) {
      wx.showToast({
        title: "正在加载",
        icon: "loading",
        mask: "ture",
        duration: 1200
      })
      animation2.translate(0, -heightWin).step({ duration: 500 })
      that.setData({
        chapterClickTops: false,
        animationData: animation2.export()
      })
    } else {
        
      animation2.translate(0, 0).step({ duration: 500 })
      that.setData({
        chapterClickTops: true,
        animationData: animation2.export()
      })
    }

  },

  //题目跳转
  topicClick(e) {
    var that = this
    // console.log(e.currentTarget.dataset.index)
    newIndex = e.currentTarget.dataset.index
  
    
    var animation2 = wx.createAnimation({});
    animation2.translate(0, 0).step({ duration: 500 })
    let arrs = that.data.topicArr
    let topicObj = arrs[newIndex]
    if (!topicObj.my_yes) {
      topicObj.my_yes = ''
    }
    let optionArr = []
    if (topicObj.item1 != '') {
      optionArr.push(topicObj.item1)
    }
    if (topicObj.item2 != '') {
      optionArr.push(topicObj.item2)
    }
    if (topicObj.item3 != '') {
      optionArr.push(topicObj.item3)
    }
    if (topicObj.item4 != '') {
      optionArr.push(topicObj.item4)
    }
    topicObj.option = optionArr
    let topicObjContent = topicObj.content//获取题目
    let topicObjKey = topicObj.key_words//获取关键字
    let falseKey = topicObj.key_false//获取关键字
    let tureKey = topicObj.key_true//获取关键字
    if (topicObjKey) {
      keyWords(that, topicObjContent, topicObjKey)
    } else {
      that.setData({
        arrTitle: [],
      })
    }

    if (falseKey != '') {
      falseKeys(that, falseKey, topicObj.option)
    } else {
      that.setData({
        falseOption: ['', '', '', '']
      })
    }
    if (tureKey != '') {
      tureKeys(that, tureKey, topicObj.option)
    } else {
      that.setData({
        tureOption: ['', '', '', '']
      })
    }
    if (arrs[e.currentTarget.dataset.index].culture == 1) {
      arrs[e.currentTarget.dataset.index].topics = ['', '', '', '']
    }
    // console.log(topicObj)
    let words = topicObj.item_true
    let newKeyArr = []
    let result_ture = []
    for (let j = 0; words.indexOf("|") + 1 > 0; j++) {
      let newKey = words.slice(0, words.indexOf("|"))
      words = words.slice(words.indexOf("|") + 1)
      newKeyArr.push(newKey)
      if (words.indexOf("|") < 0) {
        newKeyArr.push(words)
      }
    }
    // 
    for (let g = 0; g < topicObj.option.length; g++) {
      let optionTrue = false
      let optionContent = ''
      for (let f = 0; f < newKeyArr.length; f++) {
        if (topicObj.option[g] == newKeyArr[f]) {
          optionTrue = true
          optionContent = newKeyArr[f]
        }
      }
      if (optionTrue) {
        result_ture.push(optionContent)
      } else {
        result_ture.push('')
      }
    }
    topicObj.result_ture = result_ture
    if (app.chapterStudy == 1) {
      if (topicObj.result || topicObj.result != '') {
        topicObj.my_yes = topicObj.result
      }
    }
    that.setData({
      topicArr: arrs,
      // topicObj: arrs[e.currentTarget.dataset.index],
      topicObj:topicObj,
      topicArrInd: e.currentTarget.dataset.index,
      chapterClickTops: true,
      animationData: animation2.export()
    })
  },
  

  //获取语音
  audioClick(e) {
    // console.log(e.currentTarget.dataset.src)
    const innerAudioContext = wx.createInnerAudioContext()
    innerAudioContext.autoplay = true
    innerAudioContext.src = e.currentTarget.dataset.src
    innerAudioContext.onPlay(() => {
      // console.log('开始播放')
    })
    innerAudioContext.onError((res) => {
      // console.log(res.errMsg)
      // console.log(res.errCode)
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(app.chapterStudy)
    var that = this
    let chapterName = ''
    let types = 1
    if (app.chapterStudy == 1){
      chapterName = "顺章练习"
      topicSuccessively(app.titleStudy, app.cardType, that)
      types = 2
    }
    if (app.chapterStudy == 2) {
      chapterName = "随机练习"
      topicRandom(app.titleStudy, app.cardType, that)
      types = 2
    }
    if (app.chapterStudy == 3) {
      chapterName = options.name
      topicSort(app.titleStudy, app.cardType, options.id, that)
      types = 1
      // console.log(options)
    }
    if (app.chapterStudy == 4) {
      chapterName = "我的错题"
      types = 1
    }
    if (app.chapterStudy == 5) {
      chapterName = "难题解析"
      types = 1
      // errorExam(that)
    }
    let widthBL = 750 / wx.getSystemInfoSync().windowWidth
    // let topicObj = that.data.topicArr[0]
    // let topicObjContent = topicObj.content//获取题目
    // let topicObjKey = topicObj.key_words//获取关键字
    // let newContent1 = topicObjContent.substring(0, topicObjContent.indexOf(topicObjKey))//获取题目
    // let newContent2 = topicObjContent.substring(topicObjContent.indexOf(topicObjKey) + topicObjKey.length)//获取关键字
    // console.log(topicObjContent)
    // console.log(topicObjKey)
    
    // console.log(newContent1, newContent2)

    newIndex = 0
    that.setData({
      heights: wx.getSystemInfoSync().windowHeight/2,
      heightx: 80/ widthBL,
      heightd: wx.getSystemInfoSync().windowHeight / 2 - (80 / widthBL),
      chapterName: chapterName,
      title:1,
      types: types
      // topicObj: that.data.topicArr[0],
      // newContent1: newContent1,
      // newContent2: newContent2,
    })
  // console.log(that.data.objNew)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.hideShareMenu()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    var indexs = newIndex
    var that = this
    that.setData({
      topicArrs:[],
      topicArr:[]
    })
    // console.log(indexs)
    if (app.chapterStudy == 2){
      wx.request({
        url: 'https://minitest.17jxyx.com/api/v1/subject/number',
        method: "POST",
        header: {
          token: app.token
        },
        data: {
          // course_id: course_id,
          // status: status,
          // choice: choice,
          car_status: app.cardType,
          course: app.titleStudy,
          number: indexs + 1
        },
        success: function (res) {
          // console.log(res)
          // console.log(res.data[3])

        },
        fail: function (res) {
          console.log(res)
        }
      })
    }
  },
 

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
//顺章练习
function topicSuccessively(course, car_status,that){
  // console.log(course, car_status,"gdashjgdshja")
  wx.request({
    url: 'https://minitest.17jxyx.com/api/v1/subject/course',
    method: "POST",
    header: {
      token: app.token
    },
    data:{
      course: course,
      car_status: car_status
    },
    success: function (res) {
      console.log(res)
      let ind = 0
      // console.log(res.data[3]) my_yes
      // if (res.data[2] != 0){
        ind = res.data[2]
        newIndex = res.data[2]
      // }

      let topicObj = res.data[3][ind]
      if (topicObj.culture == 1 && (topicObj.result || topicObj.result != '')){
        topicObj.my_yes = JSON.parse(topicObj.result)
      }else{
        topicObj.my_yes = topicObj.result
      }
      let optionArr = []
      if (topicObj.item1 != ''){
        optionArr.push(topicObj.item1)
      }
      if (topicObj.item2 != '') {
        optionArr.push(topicObj.item2)
      }
      if (topicObj.item3 != '') {
        optionArr.push(topicObj.item3)
      }
      if (topicObj.item4 != '') {
        optionArr.push(topicObj.item4)
      }
      topicObj.option = optionArr
      let topicObjContent = topicObj.content//获取题目
      let topicObjKey = topicObj.key_words//获取关键字
      let falseKey = topicObj.key_false//获取关键字
      let tureKey = topicObj.key_true//获取关键字

      if (topicObjKey) {
        keyWords(that, topicObjContent, topicObjKey)
      } else {
        that.setData({
          arrTitle: [],
        })
      }

      if (falseKey != '') {
        falseKeys(that, falseKey, res.data[3][ind].option)
      } else {
        that.setData({
          falseTitle: [],
        })
      }
      if (tureKey != '') {
        tureKeys(that, tureKey, res.data[3][ind].option)
      } else {
        that.setData({
          tureTitle: [],
        })
      }
      let arrs = res.data[3]
      if (arrs[0].culture == 1) {
        arrs[0].topics = ['', '', '', '']
      }
      // console.log(topicObj)
      let words = topicObj.item_true
      let newKeyArr = []
      let result_ture = []
      for (let j = 0; words.indexOf("|") + 1 > 0; j++) {
        let newKey = words.slice(0, words.indexOf("|"))
        words = words.slice(words.indexOf("|") + 1)
        newKeyArr.push(newKey)
        if (words.indexOf("|") < 0) {
          newKeyArr.push(words)
        }
      }
      // 
      for (let g = 0; g < topicObj.option.length; g++) {
        let optionTrue = false
        let optionContent = ''
        for (let f = 0; f < newKeyArr.length; f++) {
          if (topicObj.option[g] == newKeyArr[f]) {
            optionTrue = true
            optionContent = newKeyArr[f]
          }
        }
        if (optionTrue) {
          result_ture.push(optionContent)
        } else {
          result_ture.push('')
        }
      }
      if (topicObj.culture == 1){
        topicObj.topics = ['', '', '', '']
      }
      topicObj.yesORno = topicObj.status
      topicObj.result_ture = result_ture
      // console.log(res.data[3])
      console.log(res.data[3])
      that.setData({
        topicYes: res.data[0],
        topicNo: res.data[1],
        topicArr: res.data[3],
        topicObj: topicObj,
        gif: "false",
        topicArrInd: res.data[2],
      })
      // let arrs0 = res.data[3]
      // let arrs1 = []
      // let arrs2 = []
      // let arrs3 = []
      // let arrs4 = []
      // let arrs5 = []

      // arrs1 = arrs0.slice(0, 300)
      // arrs2 = arrs0.slice(300, 600)
      // arrs3 = arrs0.slice(600,900)
      // arrs4 = arrs0.slice(900, 1200)
      // arrs5 = arrs0.slice(1200)
      // // console.log(arrs0.slice(2200))
      // that.setData({
      //   arrs1: arrs1,
      //   arrs2: arrs2,
      //   arrs3: arrs3,
      //   arrs4: arrs4,
      //   arrs5: arrs5,
      //   topicArrs: arrs,
      // })
    },
    fail: function (res) {
      console.log(res)
    }
  })
}



//关键字
function keyWords(that, texts, wordss) {
  let words = wordss;
  let contents = texts;
  let arrTitle = []
  // console.log(words)
  // console.log(words, words.indexOf(","))
  let newKeyArr = []
  if (words.indexOf(",") > 0) {
    for (let j = 0; words.indexOf(",") + 1 > 0; j++) {
      let newKey = words.slice(0, words.indexOf(","))
      words = words.slice(words.indexOf(",") + 1)
      newKeyArr.push(newKey)
      if (words.indexOf(",") < 0) {
        newKeyArr.push(words)
      }
    }
  } else {
    newKeyArr.push(words)
  }

  // console.log(newKeyArr)
  for (let i = 0; i < newKeyArr.length; i++) {
    let obj1 = {
      content: contents.substring(0, contents.indexOf(newKeyArr[i])),
      font: 1
    }
    arrTitle.push(obj1)
    let obj2 = {
      content: newKeyArr[i],
      font: 2
    }
    arrTitle.push(obj2)
    if (i + 1 == newKeyArr.length) {
      let obj3 = {
        content: contents.substring(contents.indexOf(newKeyArr[i]) + newKeyArr[i].length),
        font: 1
      }
      arrTitle.push(obj3)
    }
    contents = contents.substring(contents.indexOf(newKeyArr[i]) + newKeyArr[i].length)
  }
  that.setData({
    arrTitle: arrTitle,
  })
}

//错误提示
function falseKeys(that, wordss, arrOption) {
  // console.log(wordss, arrOption)
  let words = wordss;
  let falseOption = ['', '', '', '']
  let newKeyArr = []
  if (words.indexOf(",") > 0) {
    for (let j = 0; words.indexOf(",") + 1 > 0; j++) {
      let newKey = words.slice(0, words.indexOf(","))
      words = words.slice(words.indexOf(",") + 1)
      newKeyArr.push(newKey)
      if (words.indexOf(",") < 0) {
        newKeyArr.push(words)
      }
    }
  } else {
    newKeyArr.push(words)
  }
  for (let k = 0; k < arrOption.length; k++) {
    let newArrs = []
    let contents = arrOption[k]
    for (let i = 0; i < newKeyArr.length; i++) {
      if (contents.indexOf(newKeyArr[i]) < 0) {
      } else {
        let obj1 = {
          content: contents.substring(0, contents.indexOf(newKeyArr[i])),
          font: 1
        }
        newArrs.push(obj1)
        let obj2 = {
          content: newKeyArr[i],
          font: 2
        }
        newArrs.push(obj2)
        contents = contents.substring(contents.indexOf(newKeyArr[i]) + newKeyArr[i].length)

      }
      if (i + 1 == newKeyArr.length) {
        let obj3 = {
          content: contents,
          font: 1
        }
        newArrs.push(obj3)
      }
    }
    falseOption[k] = newArrs
  }

  that.setData({
    falseOption: falseOption

  })
}
//正确提示
function tureKeys(that, wordss, arrOption) {
  // console.log(wordss, arrOption)
  let words = wordss;
  let tureOption = ['', '', '', '']
  let newKeyArr = []
  if (words.indexOf(",") > 0) {
    for (let j = 0; words.indexOf(",") + 1 > 0; j++) {
      let newKey = words.slice(0, words.indexOf(","))
      words = words.slice(words.indexOf(",") + 1)
      newKeyArr.push(newKey)
      if (words.indexOf(",") < 0) {
        newKeyArr.push(words)
      }
    }
  } else {
    newKeyArr.push(words)
  }
  for (let k = 0; k < arrOption.length; k++) {
    let newArrs = []
    let contents = arrOption[k]
    for (let i = 0; i < newKeyArr.length; i++) {
      if (contents.indexOf(newKeyArr[i]) < 0) {
      } else {
        let obj1 = {
          content: contents.substring(0, contents.indexOf(newKeyArr[i])),
          font: 1
        }
        newArrs.push(obj1)
        let obj2 = {
          content: newKeyArr[i],
          font: 2
        }
        newArrs.push(obj2)
        contents = contents.substring(contents.indexOf(newKeyArr[i]) + newKeyArr[i].length)
      }
      if (i + 1 == newKeyArr.length) {
        let obj3 = {
          content: contents,
          font: 1
        }
        newArrs.push(obj3)
      }
    }
    tureOption[k] = newArrs

  }
  // console.log(tureOption, "tureOption")
  that.setData({
    tureOption: tureOption
  })
}

//答案提交
function answer(course_id, status, choice, indexs) {
  // console.log(course_id, status, choice)
  //章节练习
  if (app.chapterStudy == 1){
    wx.request({
      url: 'https://minitest.17jxyx.com/api/v1/subject/judge',
      method: "POST",
      header: {
        token: app.token
      },
      data: {
        course_id: course_id,
        status: status,
        choice: choice,
        car_status: app.cardType,
        course: app.titleStudy,
        num: indexs + 1
      },
      success: function (res) {
        // console.log(res)
        // console.log(res.data[3])

      },
      fail: function (res) {
        console.log(res)
      }
    })
  }
}
//错题提交
function errors(course_id) {
  if (app.chapterStudy != 1) {
    wx.request({
      url: 'https://minitest.17jxyx.com/api/v1/subject/refer',
      method: "POST",
      header: {
        token: app.token
      },
      data: {
        course_id: course_id,
        car_status: app.cardType,
        course: app.titleStudy,
      },
      success: function (res) {
        // console.log(res)
      },
      fail: function (res) {
        console.log(res)
      }
    })
  }
}


//随机练习
function topicRandom(course, car_status, that) {
  // console.log(course, car_status, "gdashjgdshja")
  wx.request({
    url: 'https://minitest.17jxyx.com/api/v1/subject/random',
    method: "POST",
    header: {
      token: app.token
    },
    data: {
      course: course,
      car_status: car_status
    },
    success: function (res) {
      console.log(res)
      let ind = 0
      // console.log(res.data[0]) my_yes
      // if (res.data[2] != 0){
      ind = res.data[1]
      newIndex = res.data[1]
      // }

      let topicObj = res.data[0][ind]
      if (topicObj.culture == 1 && (topicObj.result || topicObj.result != '')) {
        topicObj.my_yes = JSON.parse(topicObj.result)
      } else {
        topicObj.my_yes = topicObj.result
      }
      let optionArr = []
      if (topicObj.item1 != '') {
        optionArr.push(topicObj.item1)
      }
      if (topicObj.item2 != '') {
        optionArr.push(topicObj.item2)
      }
      if (topicObj.item3 != '') {
        optionArr.push(topicObj.item3)
      }
      if (topicObj.item4 != '') {
        optionArr.push(topicObj.item4)
      }
      topicObj.option = optionArr
      let topicObjContent = topicObj.content//获取题目
      let topicObjKey = topicObj.key_words//获取关键字
      let falseKey = topicObj.key_false//获取关键字
      let tureKey = topicObj.key_true//获取关键字

      if (topicObjKey) {
        keyWords(that, topicObjContent, topicObjKey)
      } else {
        that.setData({
          arrTitle: [],
        })
      }

      if (falseKey != '') {
        falseKeys(that, falseKey, res.data[0][ind].option)
      } else {
        that.setData({
          falseTitle: [],
        })
      }
      if (tureKey != '') {
        tureKeys(that, tureKey, res.data[0][ind].option)
      } else {
        that.setData({
          tureTitle: [],
        })
      }
      let arrs = res.data[0]
      if (arrs[0].culture == 1) {
        arrs[0].topics = ['', '', '', '']
      }
      // console.log(topicObj)
      let words = topicObj.item_true
      let newKeyArr = []
      let result_ture = []
      for (let j = 0; words.indexOf("|") + 1 > 0; j++) {
        let newKey = words.slice(0, words.indexOf("|"))
        words = words.slice(words.indexOf("|") + 1)
        newKeyArr.push(newKey)
        if (words.indexOf("|") < 0) {
          newKeyArr.push(words)
        }
      }
      // 
      for (let g = 0; g < topicObj.option.length; g++) {
        let optionTrue = false
        let optionContent = ''
        for (let f = 0; f < newKeyArr.length; f++) {
          if (topicObj.option[g] == newKeyArr[f]) {
            optionTrue = true
            optionContent = newKeyArr[f]
          }
        }
        if (optionTrue) {
          result_ture.push(optionContent)
        } else {
          result_ture.push('')
        }
      }
      if (topicObj.culture == 1) {
        topicObj.topics = ['', '', '', '']
      }
      topicObj.yesORno = topicObj.status
      topicObj.result_ture = result_ture
      that.setData({
        topicYes: 0,
        topicNo: 0,
        topicArr: arrs,
        topicObj: topicObj,
        gif: "false",
        topicArrInd: res.data[1]
      })
      // let arrs0 = arrs
      // let arrs1 = []
      // let arrs2 = []
      // let arrs3 = []
      // let arrs4 = []
      // let arrs5 = []

      // arrs1 = arrs0.slice(0, 300)
      // arrs2 = arrs0.slice(300, 600)
      // arrs3 = arrs0.slice(600, 900)
      // arrs4 = arrs0.slice(900, 1200)
      // arrs5 = arrs0.slice(1200)
      // that.setData({
      //   arrs1: arrs1,
      //   arrs2: arrs2,
      //   arrs3: arrs3,
      //   arrs4: arrs4,
      //   arrs5: arrs5,
      // })
    },
    fail: function (res) {
      console.log(res)
    }
  })
}

//技巧练习
function topicSort(course, car_status, sort, that) {
  // console.log(course, car_status, "gdashjgdshja")
  wx.request({
    url: 'https://minitest.17jxyx.com/api/v1/subject/sort',
    method: "POST",
    header: {
      token: app.token
    },
    data: {
      course: course,
      car_status: car_status,
      id: sort
    },
    success: function (res) {
      // console.log(res)
      let ind = 0
      // console.log(res.data[0]) my_yes
      // if (res.data[2] != 0){
      // ind = res.data[1]
      newIndex = 0
      // }

      let topicObj = res.data[0]
      if (topicObj.culture == 1 && (topicObj.result || topicObj.result != '')) {
        topicObj.my_yes = JSON.parse(topicObj.result)
      } else {
        topicObj.my_yes = topicObj.result
      }
      let optionArr = []
      if (topicObj.item1 != '') {
        optionArr.push(topicObj.item1)
      }
      if (topicObj.item2 != '') {
        optionArr.push(topicObj.item2)
      }
      if (topicObj.item3 != '') {
        optionArr.push(topicObj.item3)
      }
      if (topicObj.item4 != '') {
        optionArr.push(topicObj.item4)
      }
      topicObj.option = optionArr
      let topicObjContent = topicObj.content//获取题目
      let topicObjKey = topicObj.key_words//获取关键字
      let falseKey = topicObj.key_false//获取关键字
      let tureKey = topicObj.key_true//获取关键字

      if (topicObjKey) {
        keyWords(that, topicObjContent, topicObjKey)
      } else {
        that.setData({
          arrTitle: [],
        })
      }

      if (falseKey != '') {
        falseKeys(that, falseKey, res.data[0].option)
      } else {
        that.setData({
          falseTitle: [],
        })
      }
      if (tureKey != '') {
        tureKeys(that, tureKey, res.data[0].option)
      } else {
        that.setData({
          tureTitle: [],
        })
      }
      let arrs = res.data
      if (arrs[0].culture == 1) {
        arrs[0].topics = ['', '', '', '']
      }
      // console.log(topicObj)
      let words = topicObj.item_true
      let newKeyArr = []
      let result_ture = []
      for (let j = 0; words.indexOf("|") + 1 > 0; j++) {
        let newKey = words.slice(0, words.indexOf("|"))
        words = words.slice(words.indexOf("|") + 1)
        newKeyArr.push(newKey)
        if (words.indexOf("|") < 0) {
          newKeyArr.push(words)
        }
      }
      // 
      for (let g = 0; g < topicObj.option.length; g++) {
        let optionTrue = false
        let optionContent = ''
        for (let f = 0; f < newKeyArr.length; f++) {
          if (topicObj.option[g] == newKeyArr[f]) {
            optionTrue = true
            optionContent = newKeyArr[f]
          }
        }
        if (optionTrue) {
          result_ture.push(optionContent)
        } else {
          result_ture.push('')
        }
      }
      if (topicObj.culture == 1) {
        topicObj.topics = ['', '', '', '']
      }
      topicObj.yesORno = topicObj.status
      topicObj.result_ture = result_ture
      // console.log(arrs)
      that.setData({
        topicYes: 0,
        topicNo: 0,
        topicArr: arrs,
        gif: "false",
        topicObj: topicObj,
        topicArrInd: 0
      })
      // let arrs0 = arrs
      // let arrs1 = []
      // let arrs2 = []
      // let arrs3 = []
      // let arrs4 = []
      // let arrs5 = []

      // arrs1 = arrs0.slice(0, 300)
      // arrs2 = arrs0.slice(300, 600)
      // arrs3 = arrs0.slice(600, 900)
      // arrs4 = arrs0.slice(900, 1200)
      // arrs5 = arrs0.slice(1200)
      // that.setData({
      //   arrs1: arrs1,
      //   arrs2: arrs2,
      //   arrs3: arrs3,
      //   arrs4: arrs4,
      //   arrs5: arrs5,
      // })
    },
    fail: function (res) {
      console.log(res)
    }
  })
}
